#!/bin/bash
#
# WPUpdater: WordPress Updater
# April 2008 http://vunite.com
#
# Project website: http://code.google.com/p/wpupdater
#
# version 1.1
#
# USAGE: wpupdater -d -v -h -u -p -s -b -t
#
# The MIT License http://www.opensource.org/licenses/mit-license.php
# 
# Copyright (c) 2008 Vunite Media LLC.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 
#

# --------[ Functions ]----------------

helpWPUpdater()
{
  echo "Usage: $0 -d -v -h -u -p -s -b -c -t"
  echo "Options: These are optional argument"
  echo " -d base directory where WordPress is installed"
  echo " -v version of WordPress to install"
  echo " -h mysql database host"
  echo " -u mysql database username"
  echo " -p mysql database password"
  echo " -s mysql schema name"
  echo " -b 1 to enable backup and 0 to disable"
  echo " -c 1 to enable cleanup and 0 to disable"
  echo " -t 1 to enable update and 0 to disable"
  exit 1
}

backupwp()
{
    # cleaup old backup file
    if [ -f wpbackup.zip ]
    then
        rm -r wpbackup.zip
    fi
   
    # zip up current environment
    zip -r wpbackup.zip *
    
    # dump database
    mysqldump --host=$mysqlHost --port=$mysqlPort --opt --user=$mysqlUser --password="$mysqlPassword" $mysqlDatabase > database.sql
    
    # add database dump into wpbackup.zip
    # and clean up temporary dump file.
    zip -u wpbackup.zip database.sql
    rm -f database.sql
}

delete_files()
{
    # remove all php files from the base folder, except wp-config.php
    # you will have to merge the changes to this file manually
    rm -f $(ls *.php | grep -v '^wp-config.php$')
    
    # remove other core WordPress files and directories.
    rm *.txt
    rm *.html
    rm -f wp-content/index.php
    rm -f wp-content/plugins/hello.php
    
    rm -f -r wp-admin
    rm -f -r wp-includes
    rm -f -r wp-content/plugins/akismet    
    rm -f -r wp-content/themes/classic
    rm -f -r wp-content/themes/default
}

cleanup()
{
    rm wp-config-sample.php
}

# ---[ Default Settings ]---
baseDir="$HOME"
backupEnabled=1
cleanupEnabled=1
updateEnabled=1


# ---[ WordPress ]---
wpRepository=http://svn.automattic.com/wordpress/tags/
wpVersion="2.6"

# ---[ MySQL ]---
mysqlHost=''
mysqlPort=3306
mysqlUser=''
mysqlPassword=''
mysqlDatabase=''

# ---[ MAIN ]---

if [ $# -lt 1 ]; then
  helpWPUpdater
fi

while getopts d:v:h:u:p:s:b:c:t: opt
do
  case "$opt" in
    d) baseDir="$HOME/$OPTARG/";;
    v) wpVersion="$OPTARG";;
    h) mysqlHost="$OPTARG";;
    u) msqlUser="$OPTARG";;
    p) mysqlPassword="$OPTARG";;
    s) mysqlDatabase="$OPTARG";;
    b) backupEnabled=$OPTARG;;
    c) cleanupEnabled=$OPTARG;;
    t) updateEnabled=$OPTARG;;
    \?) helpWPUpdater;;
  esac
done

if [ -d $baseDir ]
then
    echo "Dir: $baseDir"
    echo "Version: $wpVersion"
    echo "Host: $mysqlHost"
    echo "User: $mysqlUser"
    echo "Schema: $mysqlDatabase"
    echo "Backup: $backupEnabled"
    echo "Cleanup: $cleanupEnabled"
    echo "Update: $updateEnabled"
    
    wpUrl="$wpRepository$wpVersion"
    
    cd $baseDir
    
    if [ $backupEnabled = 1 ]
    then
        backupwp
    fi
    
    if [ $updateEnabled = 1 ]
    then
        delete_files            
        svn export $wpUrl . --force
    fi    
    
    if [ $cleanupEnabled = 1 ]
    then
        cleanup
    fi
    
    echo "----------UPDATED-------------"
    
    exit 0
else
    "Direcotry $baseDir does not exist!"
fi
